package com.ctrip.ec.api.admin.controller.activity;
import com.ctrip.ec.api.admin.model.activity.ActivityModel;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.dao.activity.ActivityImageDao;
import com.ctrip.ec.biz.dao.dict.DictDao;
import com.ctrip.ec.biz.dao.forward.ForwardActivityDao;
import com.ctrip.ec.biz.entity.activity.ActivityGoodsEntity;
import com.ctrip.ec.biz.entity.activity.ActivityImageEntity;
import com.ctrip.ec.biz.entity.dict.DictCategory;
import com.ctrip.ec.biz.entity.dict.DictEntity;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.activity.ActivityDao;
import com.ctrip.ec.biz.entity.activity.ActivityEntity;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("activity")
public class ActivityController{
    @Autowired
    private ActivityDao activity;

    @Autowired
    private ForwardActivityDao forwardActivity;

    @Autowired
    private DictDao dict;

    @Autowired
    private ActivityImageDao activityImage;

    @GetMapping("list")
    public List<ActivityEntity> list() throws SQLException {
        List<ActivityEntity> list = activity.list();
        return list;
    }

    @PostMapping("list")
    public List<ActivityEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        setting.appendWhere("agent_id=" + RequestContext.getAgentId());

        List<ActivityEntity> list = activity.list(setting);
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @GetMapping("detail/{id}")
    public  ActivityEntity detail(@PathVariable Long id) throws SQLException {
        return activity.entity(id);
    }

    @PostMapping("save")
    public  Long save(@RequestBody ActivityModel entity) throws SQLException {

        DictEntity dictEntity = dict.entity(entity.getCategoryId());
        if(dictEntity!= null){
            entity.setCategoryName(dictEntity.getName());
        }
        entity.setAgentId(RequestContext.getAgentId());
        activity.save(entity);

        if(entity.getImageList()!=null){
            for(ActivityImageEntity imageEntity : entity.getImageList()){
                imageEntity.setActivityId( entity.getId());
                activityImage.save(imageEntity);
            }
        }
        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        forwardActivity.deleteByActivityIds(ids);
        activity.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        activity.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        activity.unerase(ids);
    }

    @GetMapping("category")
    public List<DictEntity> category() throws SQLException {
        List<DictEntity> list  = dict.list(DictCategory.activity_type);
        return list;
    }

}
